package View;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import Core.Game;
import Core.Position;
import Core.Symbol;
import Model.Morpion;
import Model.MorpionSymbole;

public class GridView extends JPanel implements Observer,MouseListener
{
    private Cell[][] cells;
    private Game model;
    private Position positionClicked;

    public GridView(Game model)
    {
	this.model = model;
	this.cells = new Cell[this.model.getBoard().getLines()][this.model
		.getBoard().getColumns()];
	this.setLayout(new GridLayout(this.model.getBoard().getLines(),
		this.model.getBoard().getColumns()));
	this.initialize(model);
	this.model.addObserver(this);
    }

    private void initialize(Game model)
    {
	for (int i = 0; i < model.getBoard().getLines(); i++)
	{
	    for (int j = 0; j < model.getBoard().getColumns(); j++)
	    {
		Cell c = new Cell(i, j);
		this.cells[i][j] = c;
		c.addMouseListener(this);
		this.add(c);
	    }
	}
    }

    public Cell[][] getCells()
    {
	return cells;
    }

    public void setCells(Cell[][] cells)
    {
	this.cells = cells;
    }

   /* public void paintComponent(Graphics g)
    {
	
	super.paintComponent(g);
	for (int i = 0; i < model.getBoard().getLines(); i++)
	{
	    for (int j = 0; j < model.getBoard().getColumns(); j++)
	    {
		Cell c = this.getCells()[i][j];
		if (model.getBoard().getSymbol(new Position(i, j))
			.equals(MorpionSymbole.CROSS))
		{
		    c.drawCross(g);
		  
		  c.repaint();
		} else if (model.getBoard().getSymbol(new Position(i, j))
			.equals(MorpionSymbole.CIRCLE))
		{
		    c.drawCircle(g);
		    //c.repaint();
		}
	    }
	}
    }*/

   // @Override
    public void update(Observable arg0, Object arg1)
    {
	System.out.println("OBverve");
	if (arg1 instanceof Position && arg0 instanceof Morpion)
	{
	    Morpion m = (Morpion) arg0;
	    Position p = (Position) arg1; 
	    Cell c = this.getCells()[p.getLine()][p.getColumn()];
	    Symbol s  = this.model.getCurrentPlayer().getSymbole();
	    System.out.println(p.toString());
	    if (s.equals(MorpionSymbole.CROSS))
	    {
		c.setForm(1);
		
	    }
	    else if (s.equals(MorpionSymbole.CIRCLE))
	    {
		c.setForm(2);
	    }
	    c.repaint();
	}
    }
    
    public Position getPositionClicked()
    {
	if (this.positionClicked == null)
	{
	    return new Position(-1,-1);
	}
	else
	{
	    return this.positionClicked;
	}
    }
    
    public void setPositionClicked(Position positionClicked)
    {
        this.positionClicked = positionClicked;
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
	Cell a = (Cell) e.getSource();
	this.positionClicked = new Position(a.getI(),a.getJ());
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
	// TODO Auto-generated method stub
	
    }

}
